//
// Created by YANHAI on 2020/3/31.
//

#pragma once

#include <exception>
#include <string>
#include <sstream>

/**
 * HTTP server 异常
 */
class HttpServerException : public std::exception {
public:
    HttpServerException() = default;

    explicit HttpServerException(const std::string &msg)
    {
        this->msg = msg;
    }

    virtual const char *what() const noexcept
    {
        return msg.c_str();
    }

protected:
    std::string msg;
};

/**
 * HTTP server 初始化异常
 */
class HttpServerInitException : public HttpServerException {
public:
    HttpServerInitException() = default;

    explicit HttpServerInitException(const std::string &msg)
    {
        this->msg = msg;
    }
};


/**
 * 请求异常
 */
class RequestException : public std::exception {
public:
    RequestException() = default;

    explicit RequestException(const std::string &msg)
    {
        this->msg = msg;
    }

    virtual const char *what() const noexcept
    {
        return msg.c_str();
    }

protected:
    std::string msg;
};

/**
 * 请求异常 -- 解析body异常
 */
class RequestParseBodyException : public RequestException {
public:
    RequestParseBodyException() = default;

    explicit RequestParseBodyException(const std::string &msg)
    {
        this->msg = msg;
    }
};

template <typename T>
class KeyError : public std::exception {
public:
public:
    explicit KeyError(const T &msg)
    {
        std::stringstream ss;
        ss << msg;
        this->msg = "KeyError: " + ss.str();
    }

    virtual const char *what() const noexcept
    {
        return msg.c_str();
    }

protected:
    std::string msg;
};
